<template>
  <div>
    <el-upload
      :class="{'img-upload': !$slots.default}"
      v-bind="$props"
      :on-success="handleSuccess"
      :before-upload="handleBeforeUpload"
      :show-file-list="false">  
        <button v-if="!$slots.default" type="button">
            <img v-if="value" :src="value" class="avatar">
            <i v-else class="el-icon-plus"></i>
        </button>
        <slot v-else></slot>
    </el-upload>
    <el-dialog v-model="dialogVisible" size="tiny">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
  import * as ui from 'l-element'
  import sha1 from 'js-sha1'
  import { mapGetters } from 'vuex'
  let props = Object.assign({}, ui.default.Upload.props)
  delete props.onSuccess
  delete props.beforeUpload
  export default {
    props: {
      ...props,
      value: {
        type: String
      },
      showFileList: {
        type: Boolean,
        default: false
      },
      headers: {
        type: Object,
        default () {
          let timestamp = new Date().getTime()
          let userId = localStorage.getItem('htxk_userId')
          let token = localStorage.getItem('htxk_token')
          let signature = sha1(userId + token + timestamp.toString())
          return {
            'X-SNS-UserId': userId,
            'X-SNS-Timestamp': timestamp,
            'X-SNS-Signature': signature,
            'X-SNS-ClientType': 1
          }
        }
      },
      accept: {
        type: String,
        default: 'image/jpeg,image/png'
      },
      size: {
        type: Number,
        default: 1024 * 1024 * 5
      }
    },
    watch: {
      value (value, old) {
        // console.log('value', value, old)
      }
    },
    computed: {
      ...mapGetters({
        global: 'global'
      })
    },
    data () {
      return {
        dialogImageUrl: '',
        dialogVisible: false
      }
    },
    methods: {
      handleSuccess (res) {
        if (res.result === '0') {
          let url = res.data
          this.$emit('change', url).$emit('success', url).$emit('input', url)
        }
      },
      handleBeforeUpload (file) {
        if (file.size > this.size) {
          this.$message.error(`上传的文件不能超过${this.size / 1024 / 1024}M`)
          return false
        }
        if (!/(.png|.jpeg|.jpg)$/.test(file.name)) {
          this.$message.error('图片格式格式不正确,请重新上传')
          return false
        }
        return true
      },
      handlePictureCardPreview (file) {
        this.dialogImageUrl = file.url
        this.dialogVisible = true
      }
    }
  }
</script>

<style lang="scss">
    @import '~styles/var.scss';

    .img-upload {
        border: 1px dashed $border-color-base;
        display: inline-block;
        height: 100px;
        width: 100px;
        vertical-align: middle;
        position: relative;
        background-color: #fafafa;
        &:hover {
            border-color: $primary-color;
            .el-icon-plus {
                font-size: 28px;
                color: $primary-color;
            }
        }
        box-sizing: border-box;
        button {
            border: 0;
            outline: none;
            position: absolute;
            left: 0;
            top: 0;
            height: 100%;
            width: 100%;
            background: transparent;
            cursor: pointer;

            .el-icon-plus {
                font-size: 28px;
                color: #8c939d;
            }
            img {
                max-width: 100%;
                max-height: 100%;
            }
        }
    }

</style>
